package com.bootcamp.servlet.cart;

/**
 * @program: wrok-1101
 * @author: Keyu Li
 * @create: 2021-11-04 19:49
 **/

import com.bootcamp.servlet.mvc.model.Product;
import com.bootcamp.servlet.util.AuthLoginUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@WebServlet(name = "GoodsDetailServlet", value = "/GoodsDetailServlet")
public class GoodsDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        // 获取商品信息
        Integer id = Integer.parseInt(request.getParameter("id"));
        List<Product> products = (List<Product>) getServletContext().getAttribute("products");
        Product pdetail = null;
        for (Product product : products) {
            if (product.getId().equals(id)) {
                pdetail = product;
                break;
            }
        }
        // 如果瞎输入的id,找不到该商品
        if (pdetail == null) {
            writer.println("<h2>Illegal product id!</h2>");
            return;
        }
        // 说明id是合法的，将当前页面加入到 历史足迹 中。
        addFootPrint(request.getSession(), id);
        // 展示首页
        AuthLoginUtils.authLoginStatus(request, writer);
        String storepage = request.getContextPath() + "/ShopStoreServlet";
        String cart = request.getContextPath() + "/ShowCartServlet";
        writer.println("<div><a href='" + storepage + "'>返回购物页面</a>&nbsp;&nbsp;&nbsp;<a href='" + cart + "'>购物车</a></div>");

        // 展示商品，并可以展示数量
        String action = request.getContextPath() + "/AddCartServlet";
        writer.println("<div>\n" +
                "    <form action=" + action + " method=\"get\">\n" +
                "        <table>\n" +
                "            <tr>\n" +
                "                <th>品牌</th>\n" +
                "                <td>" + pdetail.getName() + "</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>颜色</th>\n" +
                "                <td>" + pdetail.getColor() + "</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>类别</th>\n" +
                "                <td>" + pdetail.getCategory() + "</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>价格</th>\n" +
                "                <td>" + pdetail.getPrice() + "</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>描述</th>\n" +
                "                <td>" + pdetail.getDescription() + "</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>数量</th>\n" +
                "                <td><input type=\"number\" name=\"count\" value='1'></td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <td>\n" +
                "                    <input type='hidden' name='id' value='" + pdetail.getId() + "'>" +
                "                </td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <td align=\"center\">\n" +
                "                    <input type=\"submit\" value=\"添加购物车\" style=\"color: chocolate;font-size: large\">\n" +
                "                </td>\n" +
                "            </tr>\n" +
                "        </table>\n" +
                "    </form>\n" +
                "</div>");
    }

    private void addFootPrint(HttpSession session, Integer id) {
        //能够存储的历史记录总数为5，显示最近数为2
        final Integer ALL_FOOT = 5;
        final Integer RECENT = 2;
        Map footprintMap = (Map) session.getAttribute("footprintMap");
        Deque footprintDeque = (Deque) session.getAttribute("footprintDeque");
        if (footprintMap == null || footprintDeque == null) {
            footprintMap = new HashMap<Integer, Date>();
            footprintDeque = new LinkedList();
            session.setAttribute("footprintMap", footprintMap);
            session.setAttribute("footprintDeque", footprintDeque);
        }
        // 如果记录中已经包含，deque中删除旧记录，在尾部插入新记录
        if (footprintMap.get(id) != null) {
            footprintDeque.remove(id);
        }
        footprintMap.put(id, new Date());
        footprintDeque.addFirst(id);
        // 如果容量超过限制，Deque删除头部节点，Map中也删除对应key
        if (footprintDeque.size() > ALL_FOOT) {
            Integer last = (Integer) footprintDeque.removeLast();
            footprintMap.remove(last);
        }
        return;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
